﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nebular.ExpenseManagement.Infrastructure;

namespace Nebular.ExpenseManagement.Model
{
    [Serializable]
    public class Principal : MarshalByRefObject, IPrincipal
    {
        readonly Guid _id;
        readonly string _name;
        protected Principal(Guid id, string name)
        {
            _id = id;
            _name = name;
        }
        protected Principal(xpPrincipal principal)
            : this(principal.Id, principal.Name.SubstringAfter("."))
        {            
        }

        #region IPrincipal Members

        public Guid Id
        {
            get { return _id; }
        }

        public string Name
        {
            get { return _name; }
        }

        #endregion

        public static IPrincipal FromExpPrincipal(xpPrincipal p)
        {
            switch (p.PrincipalType)
            { 
                case "u":
                    return new User(p);
                case "g":
                    return new Group(p);
                default:
                    throw new ArgumentException("PrincipalType must be either u or g", "p");
            }
        }
    }
}
